package com.zyb.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commons.page.PageData;
import com.zyb.commons.page.PageResult;
import com.zyb.commons.service.impl.CrudServiceImpl;
import com.zyb.product.dao.AttrGroupDao;
import com.zyb.product.dto.AttrGroupDTO;
import com.zyb.product.entity.AttrGroupEntity;
import com.zyb.product.entity.AttrGroupVO;
import com.zyb.product.service.AttrGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 属性分组
 *
 * @author 赵毅博 593163583@qq.com
 * @since 1.0.0 2022-07-15
 */
@Service
public class AttrGroupServiceImpl extends CrudServiceImpl<AttrGroupDao, AttrGroupEntity, AttrGroupDTO> implements AttrGroupService {
    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public QueryWrapper<AttrGroupEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public PageResult<AttrGroupDTO> queryAllLimit(AttrGroupVO vo) {
        Long catId=vo.getCatId(); Integer pageNum= vo.getPageNo();
        Integer pageSize= vo.getPageSize(); Long attrGroupId = vo.getAttrGroupId();
        if (pageNum == null || pageSize == null) {
            throw new IllegalArgumentException("pageNum或pageSize不能为null");
        }
        PageResult<AttrGroupDTO> pageResult = new PageResult<>(pageNum, pageSize);
        Integer total = attrGroupDao.countAll(catId,attrGroupId);
        pageResult.setTotalCount(total);
        List<AttrGroupDTO> list = attrGroupDao.queryAllLimit(catId,attrGroupId, pageResult);
        pageResult.setResult(list);
        return pageResult;

    }
}